using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.SupportingMaterials
{
    public partial class ASupportingMaterials : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // LoadAllOrganisation();
                var filter = new ASupportingMaterialFilter();
                ViewState["filter"] = filter;
            }
        }

        /// <summary>
        /// Handles the Command event of the ButtonInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonInactive_Command(object sender, CommandEventArgs e)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Handles the Command event of the ButtonSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonSave_Command(object sender, CommandEventArgs e)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Handles the Command event of the ButtonLookup control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonLookup_Command(object sender, CommandEventArgs e)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Handles the Selecting event of the ObjectDataSource1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs"/> instance containing the event data.</param>
        protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as ASupportingMaterialFilter;
            e.InputParameters["filter"] = filter;
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBoxInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as ASupportingMaterialFilter;
            if (filter != null)
            {
                filter.IsActive = CheckBoxInactive.Checked;
                GridViewSupportingMaterials.DataBind();
            }
        }

        /// <summary>
        /// Handles the Command event of the LinkButtonUrl control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void LinkButtonUrl_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.Equals("True"))
            {
                Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                                "~/Organization/EditASupportingMaterials.aspx?id={0}", e.CommandName));
            }
            else
            {
                ASupportingMaterialBO.UpdateStatus(int.Parse(e.CommandName, CultureInfo.CurrentCulture), true);
                Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                                "~/Organization/EditASupportingMaterials.aspx?id={0}", e.CommandName));
            }
        }

        /// <summary>
        /// Handles the RowCreated event of the GridViewSupportingMaterials control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
        protected void GridViewSupportingMaterials_RowCreated(object sender, GridViewRowEventArgs e)
        {
            var supportingMaterial = e.Row.DataItem as SupportingMaterial;
            if (supportingMaterial != null)
            {
                if (!supportingMaterial.IsActive)
                {
                    e.Row.Attributes["onclick"] = "return Confirm_Edit();";
                }
            }
        }
    }
}